package com.cw.controller;


import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cw.annotation.*;
import com.cw.common.api.ResultObject;
import com.cw.common.constant.*;
import com.cw.entity.*;
import com.cw.service.*;
import com.cw.util.HttpUtil;
import com.cw.util.UUIDUtil;
import io.swagger.annotations.*;

import java.text.SimpleDateFormat;
import java.util.*;

import org.apache.poi.sl.draw.geom.Guide;
import org.springframework.beans.factory.annotation.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ly
 * @since 2021-11-11
 */
@Api(tags = {"院前备注按钮"})
@RestController
@RequestMapping("/yq-beizhu")

public class YqBeizhuController {

    @Autowired
    private IYqBeizhuService targetService;
    
    @Autowired
    private IMedicalInsuranceLogService medicalInsuranceLogService;


    @PostMapping("/saveOrUpdate")
    @ApiOperation("新建或编辑")
    @ResultObjectAnnotation(value = DmlTypeConstant.INSERT, successMessage = "新建或编辑成功!", errorMessage = "新建或编辑失败!")
    public Object saveOrUpdate(@RequestBody YqBeizhu entity) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        entity.setCreateTime(new DateTime());
        String name = HttpUtil.getRequestHeaderInfo(Constants.USER_NAME);
        name = name == null || name.isEmpty() ? "System" : name;
        entity.setCreateBy(name);
        entity.setId(UUIDUtil.uuid());
        return targetService.saveOrUpdate(entity);
    }

    /**
     * 查询备注信息
     * xnd
     * 2021-11-15
     *
     * @param preid
     * @return
     */
    @ApiOperation("查询备注")
    @PostMapping("/SelectBeiZhu")
    public Object SelectBeiZhu(@RequestBody YqBeizhu preid) {
        return ResultObject.successReturn(targetService.SelectBeiZhu(preid));
    }

    /**
     * 修改医保类型
     */
    @ApiOperation("修改医保类型")
    @PostMapping("/updateYBLX")
    public ResultObject updateYBLX(String name, String preid, Integer type) {
        int i = targetService.updateYBLX(name, preid, type);
        return i != 0 ? ResultObject.successReturn("修改成功"):ResultObject.errorReturn("修改失败");
    }

    /**
     * 撤销修改医保
     */
    @ApiOperation("撤销修改医保")
    @PostMapping("/revokeUpdate")
    public ResultObject revokeUpdate(String preid){
        LambdaQueryWrapper<MedicalInsuranceLog> logLambdaQueryWrapper = new LambdaQueryWrapper<>();
        logLambdaQueryWrapper.eq(MedicalInsuranceLog::getPreid,preid);
        boolean remove = medicalInsuranceLogService.remove(logLambdaQueryWrapper);
        return remove ? ResultObject.successReturn("修改成功"):ResultObject.errorReturn("修改失败");
    }

}

